package com.xiaobange.controller.system;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.xiaobange.dto.ReservationListPage;
import com.xiaobange.entity.Reservation;
import com.xiaobange.entity.User;
import com.xiaobange.mapper.ReservationMapper;
import com.xiaobange.service.ReservationService;
import com.xiaobange.vo.DoctorInfo;
import com.xiaobange.vo.RequestPage;
import com.xiaobange.vo.ResultResponse;
import jakarta.validation.Valid;
import jakarta.websocket.server.PathParam;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Author 小班哥
 * @Date 2025/5/15 10:13
 * @Explain 系统用户预约操作接口
 */
@RestController
@RequestMapping("/sys/appointment")
public class SysAppointmentController {

    @Autowired
    private ReservationService reservationService;

    @Autowired
    private ReservationMapper mapper;

    /**
     * 预约挂号
     * @param reservation
     * @return
     */
    @PostMapping("/addAppointment")
    @SaCheckLogin
    public ResultResponse<?> reservation(@RequestBody Reservation reservation) {
        return reservationService.addRegisteredOrder(reservation);
    }


    /**
     * 返回所有医生部分信息（作为展示）
     * @return
     */
    @GetMapping("/doctorList")
    public ResultResponse<List<DoctorInfo>> doctorList() {
        List<DoctorInfo> doctorInfos = mapper.doctorList();
        return ResultResponse.success(doctorInfos);
    }

    /**
     * 查询日期可用的预约时间
     * @param localDate 日期
     * @return 可用时间
     */
    @GetMapping("/availableTime")
    public ResultResponse<List<LocalTime>> availableTime(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate localDate,
                                                         @RequestParam("doctorId") String doctorId) {
        if (localDate == null || doctorId == null) return ResultResponse.error("参数不能为空");
        return reservationService.availableTime(localDate,doctorId);
    }


    /**
     * 分页查询预约记录
     * @param reservationListPage
     * @return
     */
    @PostMapping("/pageList")
    @ResponseBody
    public ResultResponse<?> reservationPage(@RequestBody @Valid ReservationListPage reservationListPage){
        return ResultResponse.success(reservationService.reservationPage(reservationListPage));
    }

    /**
     * 查询指定医生当日的接诊数据
     * @return
     */
    @RequestMapping("specifyDoctor")
    public ResultResponse<?> specifyDoctor(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate appointmentTime){
        return ResultResponse.success(reservationService.specifyDoctor(appointmentTime));
    }


    /**
     * 修改
     * @param reservation 修改预约信息
     * @return
     */
    @PostMapping("/updateAppointment")
    public ResultResponse<?> update(@RequestBody @Valid Reservation reservation){
        return reservationService.modify(reservation);
    }

}
